package com.glory.hzlionpda.activity;

import android.Manifest;

import com.glory.hzlionpda.R;
import com.glory.hzlionpda.base.BasePollingActivity;
import com.glory.hzlionpda.base.Constant;
import com.glory.hzlionpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_DownloadFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabCountEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_VersionEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class HomeActivity extends BasePollingActivity implements EasyPermissions.PermissionCallbacks {
    public static final int RC_WRITE_AND_READ = 001;
    private FreeUI_DownloadFragmentDialog dialog;
    private FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog;

    @Override
    protected void initData() {
        try {
            InputStream inputStream = getApplicationContext().getResources().getAssets().open("json.txt");
            String json = FreeApi_StringUtils.getString(inputStream);
            List<FreeUI_TabCountEntity> entity = new Gson().fromJson(json, new TypeToken<List<FreeUI_TabCountEntity>>() {
            }.getType());
            setHomeActivity(0, getBytesList(entity));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (dialog == null) {
            if (android.os.Build.VERSION.SDK_INT >= 23) {
                requestMyPermissions();
            } else {
                requestUpdate();
            }
        }
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @AfterPermissionGranted(RC_WRITE_AND_READ)
    private void requestMyPermissions() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, "请授予权限，否则应用将无法正常使用",
                    RC_WRITE_AND_READ, perms);
        } else {
            requestUpdate();
        }
    }

    private void requestUpdate() {
        NetUtils.requestUpdate(new FreeUI_EntityCallBack<FreeUI_VersionEntity>(
                new TypeToken<FreeUI_VersionEntity>() {
                }.getType()) {
            @Override
            public void onSuccess(Response<FreeUI_VersionEntity> response) {
                FreeUI_VersionEntity versionEntity = response.body();
                if (Integer.parseInt(versionEntity.getVersion()) > Constant.VERSION_CODE) {
                    versionEntity.setApkName(getString(R.string.app_name));
                    try {
                        versionEntity.setDesc(URLEncoder.encode(versionEntity.getDesc(), "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    versionEntity.setCurrentVersion(String.valueOf(Constant.VERSION_CODE));
                    dialog = new FreeUI_DownloadFragmentDialog();
                    dialog.setData(versionEntity);
                    dialog.show(getSupportFragmentManager(), "dialog_down_new");
                }
            }

            @Override
            public void onError(Response<FreeUI_VersionEntity> response) {
                super.onError(response);
                loadError(response.getException(), "requestUpdate");
                freeUI_generalFragmentDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(getApplicationContext(), "重新获取", "继续使用", "获取版本信息失败，请重新获取。",
                        new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                requestUpdate();
                                freeUI_generalFragmentDialog.dismiss();
                            }
                        }, new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                freeUI_generalFragmentDialog.dismiss();
                            }
                        });
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "freeUI_generalFragmentDialog");
            }
        });
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> list) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> list) {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}
